﻿using UnityEngine;
using UnityEngine.AI;

namespace BridgeShips.AI {
    [System.Serializable]
    public class EntityMovement {
        public Vector3 CurrentDestination { get { return m_CurrentDestination; } }

        public ET.AIMovementState MovementState { get { return m_MovementState; } }

        //public NavMeshAgent Agent { get { return m_Agent; } }

        [SerializeField]
        [Tooltip( "Normal speed the agent will use." )]
        private float m_DriveSpeed;

        [SerializeField]
        [Tooltip( "Distance-vale need to be stopped." )]
        private float m_StoppingDistance;

        private float m_RemainDistance = 1000;
        private Vector3 m_CurrentDestination;
        private ET.AIMovementState m_MovementState;

        private AIShip m_Ship;
        //private NavMeshAgent m_Agent;


        public void Initialize( AIShip ship ) {
            m_Ship = ship;

            //m_Agent = m_Ship.GetComponent<NavMeshAgent>();
        }

        public void Update( Transform transform ) {

            if (m_Ship != null) {
                var shipPosition = m_Ship.Settings.Detection.EntityTransform().position;
                m_CurrentDestination.y = 7.0f;
                shipPosition = m_CurrentDestination - shipPosition;
                m_RemainDistance = shipPosition.magnitude;

                Vector3 nextPosition = shipPosition.normalized * 0.1f;
                transform.position = transform.TransformPoint( nextPosition );
            }
        }

        /// <summary>
        /// Function called from different actions that allows the movement of the AI from one point to another.
        /// </summary>
        /// <param name="position"></param>
		public NavMeshPath MoveTo( Vector3 position, bool fastMove = false ) {
            var path = new NavMeshPath();

            //m_Agent.SetDestination(position);

            //We assign the current target.
            m_CurrentDestination = position;
            m_MovementState = ET.AIMovementState.AIDriving;

            return path;
        }

        public void MoveTo( Vector3 position ) {

            //We assign the current target.
            m_CurrentDestination = position;

        }

        private void ChangeMovementState( float speed, string animName, bool animValue, ET.AIMovementState newState ) {
            m_MovementState = newState;
        }

        /// <summary>
        /// Help function called from some actions that allows to check if the AI has already arrived it's current target point(m_CurrentDestination).
        /// </summary>
        /// <returns></returns>
        public bool ReachedDestination( bool isStop = true ) {
            if (m_RemainDistance <= m_StoppingDistance) {
                if (isStop) {
                    m_MovementState = ET.AIMovementState.Idle;
                }
                return true;
            }
            return false;
        }
    }
}